package org.example.covid.entity;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class CovidPartitionEntity implements WritableComparable<CovidPartitionEntity> {
    private String date;
    private String county;
    private String state;

    public CovidPartitionEntity(String date, String county, String state) {
        this.date = date;
        this.county = county;
        this.state = state;
    }

    public void set(String date, String county, String state) {
        this.date = date;
        this.county = county;
        this.state = state;
    }

    public CovidPartitionEntity() {
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getCounty() {
        return county;
    }

    public void setCounty(String county) {
        this.county = county;
    }


    @Override
    public String toString() {
        return date + "\t" + county + "\t" + state ;
    }

    /**
     * todo 根据date进行排序，date小的在前面，相同则按州排序，再相同按县排序
     * 正序
     * a1.compareTo(a2)
     * a1 > a2,  1      a1排在后面
     * a1 == a2, 0
     * a1 < a2,  -1     a1排在前面
     * @param o
     * @return
     */
    @Override
    public int compareTo(CovidPartitionEntity o) {
        int result;
        int i = this.date.compareTo(o.getDate());
        if (i > 0) {
            result = 1;
        } else if (i < 0) {
            result =-1;
        } else {
            int j = this.state.compareTo(o.getState());
            if( j > 0){
                result = 1;
            } else if(j < 0){
                result = -1;
            } else {
                return this.county.compareTo(o.county);
            }
        }
        return result;

    }


    /**
     * date,county,state,fips,cases,deaths
     * @param out
     * @throws IOException
     */
    @Override
    public void write(DataOutput out) throws IOException {
        out.writeUTF(date);
        out.writeUTF(county);
        out.writeUTF(state);
    }

    @Override
    public void readFields(DataInput in) throws IOException {
        this.date = in.readUTF();
        this.county = in.readUTF();
        this.state =in.readUTF();
    }


}
